//
//  LoginViewController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "LoginViewController.h"
#import "AkuNetWorkTool.h"
#import "HomeViewController.h"
#import "JASidePanelController.h"
#import "LeftViewController.h"
#define ACCOUNT @"account"
#define PASSWORD @"password"

@interface LoginViewController ()

@property (nonatomic, strong) UIImageView * logoView;//logo
@property (nonatomic, strong) UIImageView * logoLabel;//logo text
@property (nonatomic, strong) UITextField * accountTF;//账户
@property (nonatomic, strong) UITextField * passwordTF;//密码
@property (nonatomic, strong) UIButton    * loginButton;//登录按钮
//@property (nonatomic,assign) BOOL isLogin;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}
#pragma mark -
#pragma mark - 搭建界面
- (void)setupUI{
    
    self.title = @"登录";
    //添加子控件
    [self.view addSubview:self.logoView];
    [self.view addSubview:self.logoLabel];
    [self.view addSubview:self.accountTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.loginButton];
    
    //设置布局
    
    CGFloat padding = (SCREEN_WIDTH==320) ? 10 : 70;
    
    [self.logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(64+padding);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(70, 90));
        
    }];
    
    [self.logoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.logoView.mas_bottom).offset(2);
        make.centerX.equalTo(self.logoView);
        make.size.mas_equalTo(CGSizeMake(70, 45));
    }];
    
    [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.logoLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.accountTF.mas_bottom).offset(20);
        make.height.mas_equalTo(35);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.passwordTF.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
}

#pragma mark -
#pragma mark - lazy
- (UIImageView *)logoView{
    if (!_logoView) {
        _logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product_img_logo_nor.png"]];
    }
    return _logoView;
}

- (UIImageView *)logoLabel{
    if (!_logoLabel) {
        _logoLabel = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product_text_logo_nor.png"]];
    }
    return _logoLabel;
}

- (UITextField *)accountTF{
    
    if (!_accountTF) {
        _accountTF = [[UITextField alloc] init];
        //先从沙盒中取数据
        NSString * account = [self getInfoFromSand:ACCOUNT];
        if (account.length != 0) {
            _accountTF.text = account;
        }else{
             _accountTF.placeholder = @"请输入账户";
        }

        _accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountTF.borderStyle = UITextBorderStyleRoundedRect;
        [_accountTF becomeFirstResponder];
    }
    return _accountTF;
}

- (UITextField *)passwordTF{
    
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc] init];
        
        //先从沙盒中取数据
        NSString * password = [self getInfoFromSand:PASSWORD];
        
        if (password.length != 0) {
            _passwordTF.text = password;
        }else{
            _passwordTF.placeholder = @"请输入密码";
        }
        
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.borderStyle = UITextBorderStyleRoundedRect;
        [_accountTF becomeFirstResponder];

    }
    return _passwordTF;
}

- (UIButton *)loginButton{
    
    if (!_loginButton) {
        
        _loginButton = [[UIButton alloc] init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.backgroundColor = kThemeColor;
        _loginButton.titleLabel.font = FONT(20);
        
        [_loginButton addTarget:self action:@selector(sentRequest:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _loginButton;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

#pragma mark -
#pragma mark - 将用户名和密码存放入沙盒中
- (void)saveAccount:(NSString *)account  withPassword:(NSString *)password{
    
    [[NSUserDefaults standardUserDefaults] setObject:account  forKey:ACCOUNT];
    [[NSUserDefaults standardUserDefaults] setObject:password  forKey:PASSWORD];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

#pragma mark -
#pragma mark - 从沙盒中取出用户名和密码
- (NSString *)getInfoFromSand:(NSString *)key{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

#pragma mark -
#pragma mark - 登录按钮的监听
- (void)sentRequest:(NSString *)url{
    
    //撤销文本框的第一响应
    [self.accountTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    
    NSString * account = self.accountTF.text;
    NSString * password = self.passwordTF.text;
    
    if ([account isEqualToString:@"37"]&&[password isEqualToString:@"37"]) {
        
        //发送请求参数
        NSDictionary * dic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
        [AkuNetWorkTool POST_RequestWithURLString:kLoginUrl params:dic successBlock:^(id responseObject) {
            
            //登录成功
            //将登录状态存入沙盒中
            [[NSUserDefaults standardUserDefaults] setObject:@1 forKey:@"isLogin"];
            

            NSDictionary * dic = (NSDictionary *)responseObject;
            NSDictionary * dd  = dic[@"data"];
            NSNumber * id  = dd[@"user_id"];
            

            if (id.integerValue == 1000089 ) {
                
                //将数据存入沙盒
                [self saveAccount:account withPassword:password];
                
                //block 回调,切花leftview 中的 loginview
                self.loginSuccess();
                
                //登录成功,跳转到home页面
                [[self centerNavigationController] popToRootViewControllerAnimated:YES];
                
            }
        } errorBlock:^(NSError *error) {
            NSLog(@"网络请求失败!");
        }];
    }

}




@end
